//
//  KLSGRecommendTool.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGRecommendTool.h"

@implementation KLSGRecommendTool

- (void)updataWithBlack:(void(^)())block withFailBlock:(void(^)())failBlock
{
    // 1.创建session对象  NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    // 2.创建网址对象
    NSURL *url = [NSURL URLWithString:kRecommendUrl];
    // 3.根据URL创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 4.建立会话 NSURLSessionDataTask
    __block KLSGRecommendTool *mySelf = self;
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (!error) {
            NSError *error = nil;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
            //            self.dataDic = [NSMutableDictionary dictionary];
            mySelf.hotDic = [NSMutableDictionary dictionary];
            mySelf.banner = [NSMutableArray array];
            if (!error) {
                
                // 解析小编推荐
                NSDictionary *editorDic = dic[@"editorRecommendAlbums"];
                NSArray *array = editorDic[@"list"];
                mySelf.titles = [NSMutableArray array];
                NSMutableArray *editorReArray = [NSMutableArray array];
                for (NSDictionary *editorTempDic in array) {
                    KLSGRecommendModel *model = [[KLSGRecommendModel alloc] init];
                    [model setValuesForKeysWithDictionary:editorTempDic];
                    [editorReArray addObject:model];
                }
                [mySelf.titles addObject:@"小编推荐"];
                [mySelf.hotDic setValue:editorReArray forKey:@"小编推荐"];
                
                // 解析热播
                NSDictionary *dictory = dic[@"hotRecommends"];
                NSArray *hotArray = dictory[@"list"];
                self.categorys = [NSMutableDictionary dictionary];
                for (NSDictionary *dic in hotArray) {
                    NSArray *lists = dic[@"list"];
                    NSString *title = dic[@"title"];
                    [self.categorys setObject:dic[@"categoryId"] forKey:title];
                    NSMutableArray *array = [NSMutableArray array];
                    for (NSDictionary *tempDic in lists) {
                        KLSGRecommendModel *model = [[KLSGRecommendModel alloc] init];
                        [model setValuesForKeysWithDictionary:tempDic];
                        [array addObject:model];
                    }
                    [self.hotDic setValue:array forKey:title];
                }
                NSMutableArray *titleArray = [NSMutableArray arrayWithArray:[mySelf.hotDic allKeys]];
                [titleArray removeObject:@"小编推荐"];
                [mySelf.titles addObjectsFromArray:titleArray];
                
                // 解析轮播图
                NSDictionary *focusDic = dic[@"focusImages"];
                NSArray *focusArray = focusDic[@"list"];
                for (NSDictionary *tempFocusDic in focusArray) {
                    if (![tempFocusDic objectForKey:@"url"]) {
                        KLSGRecommendModel *model = [[KLSGRecommendModel alloc] init];
                        [model setValuesForKeysWithDictionary:tempFocusDic];
                        [mySelf.banner addObject:model];
                    }
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    block(); // block回调
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failBlock(); // block回调
                });
            }
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                failBlock(); // block回调
            });
        }
    }];
    
    // 6.启动解析
    [task resume];
    
}

- (KLSGRecommendModel *)getModelWithIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = self.titles[indexPath.section];
    NSArray *values = self.hotDic[key];
    KLSGRecommendModel *model = values[indexPath.row];
    return model;
}

- (void)setupEditorReDataWithUrl:(NSString *)urlStr  successBlock:(void(^)(NSData *data))block failBlock:(void(^)())failBlock
{
    // 1.创建session对象  NSURLSession
    NSURLSession *session = [NSURLSession sharedSession];
    // 2.创建网址对象
    NSURL *url = [NSURL URLWithString:urlStr];
    // 3.根据URL创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 4.建立会话 NSURLSessionDataTask
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error != nil) {
            // 加载数据失败
            dispatch_async(dispatch_get_main_queue(), ^{
                failBlock(); // block回调
            });
        } else {
            // 加载数据成功
            dispatch_async(dispatch_get_main_queue(), ^{
                block(data);
            });
        }
    }];
    
    // 6.启动解析
    [task resume];
}


@end
